using UnityEngine;
using System.Collections;

// We get coordinates for the sphere from the skeleton and map them to our grid
public class SpherePosUpdate : MonoBehaviour {

	public float damping = 5.0f;
	public Vector3 Offset = new Vector3(0f,0f,0f);
	public Vector2 area = new Vector2(10,10);
	
	private Vector3 desiredPos;
	
	void Start () {
		desiredPos = transform.localPosition;
	}
	
	void Update () {
		
		desiredPos = transform.localPosition;
		
		GameObject hand = GameObject.Find("Hand");
		GameObject torso = GameObject.Find("Torso");
		
		// Position of the hand in relation to the torso + offset
		Vector3 pos = hand.transform.position - torso.transform.position + Offset;
		
		pos = KeepInArea(pos);
		
		transform.localPosition = Vector3.Lerp(pos, desiredPos, damping * Time.deltaTime);
	}
	
	Vector3 KeepInArea(Vector3 vec)
	{	
		// We move in x and z axes and use y for the gesture
		return new Vector3(Mathf.Clamp(vec.x, -0.5f * area.x, 0.5f * area.x),
		                   vec.y,
		                   Mathf.Clamp(vec.z,0f,area.y));
	}
}


